<?php
/**老师查看模考*/

require __DIR__ . '/includes/utils.php';

class TeacherCheckMokao implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        $offset = isset($queryArgs['page']) ? ($queryArgs['page'] - 1) * 10 : 0;
        $sql = 'SELECT `id`, `publish_date` FROM `qb_paper_mokao` ORDER BY `publish_date` DESC LIMIT :offset,10';
        $history = $cmsdb->getResults($sql,array(':offset'=>$offset));

        //mini模考设置
        $miniMokaoList = array();
        foreach ($history as $day) {
            $miniMockInfo = $todayMokao = $cmsdb->getRow('SELECT `id`, `name` FROM `qb_paper_mokao` WHERE `publish_date`=:date',
                array(':date' => $day->publish_date));

            $miniMokaoList[] = array(
                'id' => intval($day->id),
                'name' => $miniMockInfo->name,
                'date' => $day->publish_date,
            );
        }

        //模考、估分设置
        $sql = "SELECT `option_value` FROM {$cmsdb->options} WHERE `option_name`='mokaoManage'";
        $mockSetting = unserialize($cmsdb->getVar($sql));
        if ($mockSetting['type'] === 'mock') {
            $data = $cmsdb->getRow("SELECT `id`, `paper_name`, `persons_num` FROM `data_test_paper`
            WHERE `id`=:mock_id", [':mock_id' => $mockSetting['papers']]);

            return genOutput(1, array(
                'mini_mokao_list' => $miniMokaoList,
                'mokao' => [
                    'id'   => (int)$data->id,
                    'name' => $data->paper_name
                ],
                'gufen_list' => []
            ));
        } else {
            $list = $cmsdb->getResults("SELECT `id`, `name` FROM `original_ztk_papers`
            WHERE `id` IN ({$mockSetting['papers']}) ORDER BY FIELD(`id`, {$mockSetting['papers']})");

            $paperList = [];
            foreach ($list as $l) {
                $paperList[] = array(
                    'id' => intval($l->id),
                    'name' => $l->name,
                );
            }

            return genOutput(1, array(
                'mini_mokao_list' => $miniMokaoList,
                'mokao' => null,
                'gufen_list' => $paperList
            ));
        }
    }
    
}